<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		ScriptPlanet.eu															/
/	Site:			http://www.scriptplanet.eu												/
/	Email:			support@scriptplanet.eu													/
/	Support:		http://www.scriptplanet.eu/support										/
/	Copyright:		2011 (c) ScriptPlanet.eu												/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0001																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla security
defined('_JEXEC') or die;
JHtml::_('behavior.tooltip');

$jConf = JFactory::getApplication();

$pstString = JRequest::getVar('spstring');
$pstType = JRequest::getVar('sptype');

$pstStartLm = JRequest::getVar('limitstart', 0);
$pstTotLm = JRequest::getVar('limit', $jConf->getCfg('list_limit'));

$prev = $pstStartLm - 1;
$next = $pstStartLm + 1;

$DataSongList = $this->letter;
$TotalPages = $this->counter;
$LastPage = $TotalPages - 1;
?>

<script>
	
	// Search Function
	$(function(){
		$( "#jspSearch" ).autocomplete({
			source: function( request, response ){
				$.ajax({
					url: "<?php echo JRoute::_('?option=com_joosam&view=data&layout=ajax&format=raw'); ?>",
					data:{ term: request.term },
					success: function(data){
						response( $.map( data, function(item){
							return{
								label: item.artist + ", " + item.title,
								hiddeninfo: item.songid,
								value: item.artist
							}
						}));
					}
				});
			},
			minLength: 3,
			select: function(event, ui){
				document.getElementById('sptype').value = 'spsearch';
				document.getElementById('spstring').value = ui.item.hiddeninfo;
				document.jmainform.submit();
			}
		});
	});
	
	// LetterClick Function
	function spletter(strng){
		document.getElementById('sptype').value = 'spletter';
		document.getElementById('spPageLimit').value = 0;
		document.getElementById('spstring').value = strng;
		document.jmainform.submit();
	}
	
	// More Info Function
	function moreInfo(sond){
		document.getElementById('sptype').value = 'spsearch';
		document.getElementById('spstring').value = sond;
		document.jmainform.submit();
	}

	// Rows on Page selector
	function PageSelector(){
		document.getElementById('sptype').value = 'spletter';
		var vall = document.getElementById('spSetPageLim').value;
		document.getElementById('spRowInPage').value = vall;
		document.getElementById('spPageLimit').value = '0';
		document.jmainform.submit();
	}

	// Go to selected Page
	function pageclick(pg){
		document.getElementById('sptype').value = 'spletter';
		var target = document.getElementById('spPageLimit');
		target.value = pg;
		document.jmainform.submit();
	}
</script>

<style type="text/css">
.spbutton{
	overflow:hidden;
	text-align:center;
	width:20px;
	margin:0;
	padding:0;
}
#spLetterfrm{
	text-align:center;
	margin:10px 0 0 0;
	padding:0;
}
.spHeader{
	border-radius:10px;
	padding:8px;
}
.spSongList{
	text-align:center;
	padding:8px;
	border-radius:10px;
	margin:10px 0 0 0;
}
.spFooterList{
	text-align:center;
	padding:8px;
	border-radius:10px;
	margin:10px 0 0 0;
}
</style>

<form name="jmainform" id="jmainform" action="<?php echo JRoute::_('index.php?view=joosam&layout=songinfo');?>" method="post">
	<input type="hidden" id="spPageLimit" name="limitstart" value="<?php echo $pstStartLm; ?>"/>
	<input type="hidden" id="spRowInPage" name="limit" value="<?php echo $pstTotLm; ?>"/>
	<input type="hidden" id="sptype" name="sptype" value=""/>
	<input type="hidden" id="spstring" name="spstring" value="<?php echo $pstString; ?>"/>
</form>

<div class="jsam_com_main">
	<div class="spHeader spBGTabel">
		<div id="spSearchfrm">
			<div class="ui-widget">
				<label for="jspSearch"><?php echo JText::_('COM_JOOSAM_COM_REQUEST_SEARCH'); ?></label>
				<input id="jspSearch" style="border-radius:4px;"/>
			</div>
		</div>
		<div id="spLetterfrm">
			<button class="spbutton" onclick="spletter('nolet');">#</button>
			<?php foreach(range('A','Z') as $letter): ?>
			<button id="jslett<?php echo $letter; ?>" class="spbutton" onclick="spletter('<?php echo $letter; ?>');"><?php echo $letter; ?></button>
			<?php endforeach; ?>
		</div>
	</div> <?php
	/**************************************************************/ ?>
	<div class="spSongList spBGTabel"><?php
if($DataSongList != ''):?>
		<table style="width:100%;">
			<thead>
				<tr>
					<th><?php echo JText::_('COM_JOOSAM_ARTIST'); ?></th>
					<th><?php echo JText::_('COM_JOOSAM_TITLE'); ?></th>
					<th><?php echo JText::_('COM_JOOSAM_INFO'); ?></th>
				</tr>
			</thead>
			<tbody><?php
				foreach($DataSongList as $p => $song):
					$cclass = $p % 2;
					$ssids = $song['ID']; ?>
					<tr class="jspBG<?php echo $cclass; ?>" >
						<td style="padding-left:5px;text-align:left;border:none;width:40%;"><?php echo htmlentities($song['artist']); ?></td>
						<td style="padding-left:5px;text-align:left;border:none;"><?php echo htmlentities($song['title']); ?></td>
						<td style="padding-right:3px;border:none; width:85px;">
							<button style="width:80px;height:22px;" onclick="moreInfo('<?php echo $ssids; ?>'); return false;"><?php echo JText::_('COM_JOOSAM_MORE_INFO'); ?></button>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	</div> <?php
	/**************************************************************/ ?>
	<div class="spFooterList spBGTabel">
		<table border="0" style="margin:0 auto 10px auto;">
			<tr style="border:none;">
				<td style="text-align:center;padding:4px 10px;border:none;">
					<?php if($pstStartLm < 1): ?>
					<a href="#" id="jspPeview" onclick="return false" readonly> <?php echo JText::_('COM_JOOSAM_COM_REQUEST_PREV'); ?> </a>
					<?php endif; ?>

					<?php if($pstStartLm > 0): ?>
					<a href="#" id="jspPeview" onclick="pageclick('<?php $prev; ?>'); return false;"> <?php echo JText::_('COM_JOOSAM_COM_REQUEST_PREV'); ?> </a>
					<?php endif; ?>
				</td>
				<td style="text-align:center;border:none;"><?php
				/*************************************************** PAGENATION PAGES */
				$counter = $TotalPages;
				$currentPage = $pstStartLm + 1;
				$over5page = $currentPage + 5;
				$page_x = $counter - 1;
				$page_xx = $counter - 2;
				if($over5page > $counter){
					$over5page = $counter;
				}
				$countStart = $currentPage - 3;
				if($countStart < 1){
					$countStart = 1;
				}
				$countEnd = $counter - 7;
				$countEndx = $counter - 6;
				$countEndxx = $counter - 5;
				
				$page1 = '<a style="padding:0 3px;" href="#" id="jspBut0" onclick="pageclick(\'0\'); return false;">1</a>';
				$page2 = '<a style="padding:0 3px;" href="#" id="jspBut1" onclick="pageclick(\'1\'); return false;">2</a>';
				$pageNoLast = '<a style="padding:0 3px;" href="#" id="jspBut'.$page_xx.'" onclick="pageclick(\''.$page_xx.'\'); return false;">'.$page_x.'</a>';
				$pageLast = '<a style="padding:0 3px;" href="#" id="jspBut'.$page_x.'" onclick="pageclick(\''.$page_x.'\'); return false;">'.$counter.'</a>';

				if($counter > 10){
					if($currentPage > 4){
						echo $page1;
					}
					if($currentPage > 5){
						echo $page2;
					}
					if($currentPage > 6){
						echo '...';
					}
					for($c=$countStart; $c <= $over5page; $c++){
						$place = $c - 1;
						echo '<a style="padding:0 3px;" href="#" id="jspBut'.$place.'" onclick="pageclick(\''.$place.'\'); return false;">'.$c.'</a>';
					}
					if($currentPage < $countEnd){
						echo '...';
					}
					if($currentPage < $countEndx){
						echo $pageNoLast;
					}
					if($currentPage < $countEndxx){
						echo $pageLast;
					}
				}
				else{
					for($c=1; $c <= $counter; $c++){
						$place = $c - 1;
						echo '<a style="padding:0 3px;" href="#" id="jspBut'.$place.'" onclick="pageclick(\''.$place.'\'); return false;">'.$c.'</a>';
					}
				}
				/*************************************************** PAGENATION PAGES */  ?>
				</td>
				<td style="text-align:center;padding:4px 10px;border:none;">
					<?php if($pstStartLm == $LastPage){ echo '
					<a href="#" id="jspNext" onclick="return false" readonly> '.JText::_("COM_JOOSAM_COM_REQUEST_NEXT").' </a>'; }
					else{ echo '
					<a href="#" id="jspNext" onclick="pageclick(\''.$next.'\'); return false;"> '.JText::_("COM_JOOSAM_COM_REQUEST_NEXT").' </a>'; } ?>
				</td>
			</tr>
		</table>
		<div class="limit" style="margin:auto; width:120px;">
			<span><?php echo JText::_('COM_JOOSAM_COM_REQUEST_DISPL'); ?> #</span>
			<select id="spSetPageLim" class="inputbox" size="1" onchange="PageSelector();">
				<option value="5">5</option>
				<option value="10">10</option>
				<option value="15">15</option>
				<option value="20">20</option>
				<option value="25">25</option>
				<option value="30">30</option>
				<option value="50">50</option>
				<option value="100">100</option>
			</select>
		</div>

<script>
	function SetPageNow(){
		document.getElementById('spSetPageLim').value = "<?php echo $pstTotLm; ?>";
	}
	window.onload = SetPageNow();
</script>	<?php
else: ?>
	<p><?php echo JText::_('COM_JOOSAM_COM_REQUEST_SINFO_NO_SONG_FOUND'); ?></p> <?php
endif;
?></div>
</div>